<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="LVS," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;类似于iptables的DNAT，但支持多目标转发。客户端和realserver不能再同一个网段，不然直接响应，不走网关 1.模块讲解&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;客户端发起请求 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#16">
<meta name="keywords" content="LVS">
<meta property="og:type" content="article">
<meta property="og:title" content="LVS 的 nat 模式">
<meta property="og:url" content="https://hcldirgit.github.io/2017/09/03/LVS/2. LVS 的 nat 模式/index.html">
<meta property="og:site_name" content="失落的乐章">
<meta property="og:description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;类似于iptables的DNAT，但支持多目标转发。客户端和realserver不能再同一个网段，不然直接响应，不走网关 1.模块讲解&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;客户端发起请求 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#16">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/LVS%E7%9A%84nat%E6%A8%A1%E5%BC%8F/01.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/LVS%E7%9A%84nat%E6%A8%A1%E5%BC%8F/02.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/LVS%E7%9A%84nat%E6%A8%A1%E5%BC%8F/03.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/LVS%E7%9A%84nat%E6%A8%A1%E5%BC%8F/04.jpeg?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/LVS%E7%9A%84nat%E6%A8%A1%E5%BC%8F/05.png?raw=true">
<meta property="og:updated_time" content="2017-09-01T08:51:27.660Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="LVS 的 nat 模式">
<meta name="twitter:description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;类似于iptables的DNAT，但支持多目标转发。客户端和realserver不能再同一个网段，不然直接响应，不走网关 1.模块讲解&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;客户端发起请求 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#16">
<meta name="twitter:image" content="https://github.com/hcldirgit/image/blob/master/LVS%E7%9A%84nat%E6%A8%A1%E5%BC%8F/01.png?raw=true">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://hcldirgit.github.io/2017/09/03/LVS/2. LVS 的 nat 模式/"/>





  <title>LVS 的 nat 模式 | 失落的乐章</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', '85*****1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?87980c**************99ec5e26fb5";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>











  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">失落的乐章</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">技术面前，永远都是学生。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-external-link"></i> <br />
            
            留言
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://hcldirgit.github.io/2017/09/03/LVS/2. LVS 的 nat 模式/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="失落的乐章">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/0.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="失落的乐章">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">LVS 的 nat 模式</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-03T02:04:46+08:00">
                2017-09-03
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><img src="https://github.com/hcldirgit/image/blob/master/LVS%E7%9A%84nat%E6%A8%A1%E5%BC%8F/01.png?raw=true" alt=""></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>类似于iptables的DNAT，但支持多目标转发。客户端和realserver不能再同一个网段，不然直接响应，不走网关</strong></p>
<h2 id="1-模块讲解"><a href="#1-模块讲解" class="headerlink" title="1.模块讲解"></a>1.模块讲解</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户端发起请求</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;nat模式中，vip机器进行ip的转发，只改变目的ip</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;res1和res2需要提供web服务(nginx或者httpd都可以)</p>
<h2 id="2-请求流程图"><a href="#2-请求流程图" class="headerlink" title="2.请求流程图"></a>2.请求流程图</h2><ol>
<li>客户端发起请求到vip机器上，vip服务器根据lvs的算法，转发给realserver服务器（改变目的ip为res1或者res2），并记录连接信息，只改变目的ip，源ip不变。</li>
<li>real-server收到request请求包之后，发现目的ip是自己的ip，处理请求，然后走网关，经过vip</li>
<li>vip收到reply包后，修改reply包的源ip地址为vip，发给客户端</li>
<li>从客户端来的属于本地连接的包，查hash表，然后转发给real-server</li>
<li>当client发送完毕以后，此次连接结束或者连接超时，lvs自动从hast表中删除此条记录；</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;数据包流程如下：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.客户端发起请求：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">source</span>      	192.168.147.1			dest    192.168.147.150:80</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在vip服务器上，eth1网卡上抓包如下：</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/LVS%E7%9A%84nat%E6%A8%A1%E5%BC%8F/02.png?raw=true" alt=""></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;2.lvs(vip)处理请求：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;vip服务器上抓包，在eth1网卡：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">source</span>		192.168.147.1:59334		dst	192.168.1.112/111:80</div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/LVS%E7%9A%84nat%E6%A8%A1%E5%BC%8F/03.png?raw=true" alt=""></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;3.realserver处理完请求以后，网关是eth0；在vip的eth0上抓包如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">source</span>             192.168.1.112              dst    192.168.147.1</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;4.包经过eth0返回vip服务器，然后经过eth1返回给客户端，源ip改成vip，目的ip不变，</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">source</span>		192.168.147.150           dst        192.168.147.1</div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/LVS%E7%9A%84nat%E6%A8%A1%E5%BC%8F/04.jpeg?raw=true" alt=""></p>
<h2 id="lvs-nat模式部署过程："><a href="#lvs-nat模式部署过程：" class="headerlink" title="lvs_nat模式部署过程："></a>lvs_nat模式部署过程：</h2><ol>
<li>（确保所有服务器的iptables   selinux都是关闭状态）</li>
<li>两个realserver上安装nginx或者http提供web服务</li>
<li>vip上安装ipvsadm </li>
<li>在vip服务器上新建lvs_nat.sh脚本，然后执行</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#! /bin/bash</span></div><div class="line"><span class="comment"># director 服务器上开启路由转发功能: </span></div><div class="line"><span class="built_in">echo</span> 1 &gt; /proc/sys/net/ipv4/ip_forward </div><div class="line"><span class="comment"># 关闭icmp的重定向</span></div><div class="line"><span class="built_in">echo</span> 0 &gt; /proc/sys/net/ipv4/conf/all/send_redirects</div><div class="line"><span class="built_in">echo</span> 0 &gt; /proc/sys/net/ipv4/conf/default/send_redirects</div><div class="line"><span class="built_in">echo</span> 0 &gt; /proc/sys/net/ipv4/conf/eth0/send_redirects</div><div class="line"><span class="built_in">echo</span> 0 &gt; /proc/sys/net/ipv4/conf/eth1/send_redirects</div><div class="line"></div><div class="line">iptables -F</div><div class="line">iptables -t nat -F</div><div class="line">iptables -t nat -X</div><div class="line"><span class="comment">#iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j MASQUERADE</span></div><div class="line"></div><div class="line">ipvsadm -C</div><div class="line">ipvsadm -A -t 192.168.147.150:80 -s rr -p 300</div><div class="line"><span class="comment">#-p 300   长链接300秒</span></div><div class="line">ipvsadm -a -t 192.168.147.150:80 -r 192.168.1.111:80 -m -w 1</div><div class="line">ipvsadm -a -t 192.168.147.150:80 -r 192.168.1.112:80 -m -w 1</div></pre></td></tr></table></figure>
<ol>
<li>realserver的网关是eth0，及vip的内网ip</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>lvs——nat模式的缺点：当流量比较高时，因为所有的流量进入和出去都要进过lvs，所以瓶颈就在lvs上，而dr模式就比这个效果好很多</strong></p>
<h2 id="待解决问题"><a href="#待解决问题" class="headerlink" title="待解决问题"></a>待解决问题</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当访问res2的时候，在111上可以抓到112:80到147.1上的包，如下：</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/LVS%E7%9A%84nat%E6%A8%A1%E5%BC%8F/05.png?raw=true" alt=""></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当访问res1时，在111上抓不到112:80到147.1上的包</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/LVS/" rel="tag"># LVS</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/03/LVS/19. LVS结合keepalived配置/" rel="next" title="LVS结合keepalived配置">
                <i class="fa fa-chevron-left"></i> LVS结合keepalived配置
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/03/LVS/21. Keepalived配置及典型应用案例/" rel="prev" title="Keepalived配置及典型应用案例">
                Keepalived配置及典型应用案例 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/0.png"
               alt="失落的乐章" />
          <p class="site-author-name" itemprop="name">失落的乐章</p>
           
              <p class="site-description motion-element" itemprop="description">失落的乐章的Blog</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/">
                <span class="site-state-item-count">627</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">38</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/hcldirgit" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-模块讲解"><span class="nav-number">1.</span> <span class="nav-text">1.模块讲解</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-请求流程图"><span class="nav-number">2.</span> <span class="nav-text">2.请求流程图</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#lvs-nat模式部署过程："><span class="nav-number">3.</span> <span class="nav-text">lvs_nat模式部署过程：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#待解决问题"><span class="nav-number">4.</span> <span class="nav-text">待解决问题</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">失落的乐章</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
